﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Frm
{
    public partial class FrmVouchDKP : Form
    {

        public Frm.FrmVouchDKPBase report;
        public FrmVouchDKP()
        {
            try
            {
                InitializeComponent();
                this.Icon = YTSK.Properties.Resources.Fly;
                report = new Frm.FrmVouchDKPBase(this.groupBox2, this.dataGridView1,
                    "YTXD_InvoicePending_View", this.toolStrip1);
                report.InitControl();
                GetGridData();
                this.btQuery.Click +=GetGridData;
                this.Resize += frmMain_Resize;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        public void GetGridData(object sender, EventArgs e)
        {
            GetGridData();
        }
        #region 获取待开票数据
        public bool GetGridData()
        {
            var f = new FrmTip();
            try
            {
                f.Show();
                var otherval =report.GetControlValue();
                f.setValue("正在获取数据，时间比较久，请耐心等候", 50);
                var url = string.Format("{0}/api/oms/YTXDInvoice/List?token={1}&{2}",
                            ServerConfig.ServerAddress, ServerConfig.GetToken(), otherval);
                var ss = Common.handleHttp.Get(url);
                var result = Common.handleJSON.DeserializeResult(ss);
                if (result.result != 1) {  MessageBox.Show("服务器错误:" + result.resultdetail); return false; }
                var data = result.GetListDic("data");
                f.setValue("格式化数据", 80);
                var _dg = this.dataGridView1;
                _dg.Rows.Clear();
                var rows = result.GetVal("TotalRecords").ObjToInt();
                if (rows > data.Count) { rows = data.Count; }
                if (rows == 0)
                { 
                    return true;
                }
                _dg.Rows.Add(rows);

                for (int i = 0; i < rows; i++)
                {
                    var dic = data[i];
                    var dgr = _dg.Rows[i];

                    for (int j = 0; j < _dg.Columns.Count; j++)
                    {
                        var colname = _dg.Columns[j].Name;
                        if (dic.Keys.Contains(colname))
                        {
                            dgr.Cells[j].Value = dic[colname];
                        }
                    }
                    dgr.Cells["selcol"].Value = (i + 1).ToString();
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message + ex.StackTrace); }
            finally { f.Close(); }
            return true;
        }
        #endregion


        #region 全选
        private bool _bSel = true;
        private void SelectAll(object sender, EventArgs e)
        {
            var dg = this.dataGridView1;
            for (int i = 0; i < dg.Rows.Count; i++)
            {
                var row = dg.Rows[i];
                row.Cells["selcol"].Value = _bSel ? "√" : (row.Index + 1).ToString();
            }
            _bSel = !_bSel;
        }

        #endregion

        #region 窗体大小
        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.Width > 20)
            { this.dataGridView1.Width = this.Width - 20; }
            if (this.Height > 200)
            {
                this.dataGridView1.Height = this.Height - 200;
            }
        }
        #endregion

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            var f = new FrmVouch();
            f.Show(this);
        }

    }
}
